package com.jxw.launcher;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.net.Uri;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.android.exoplayer2.C;
import com.jxw.launcher.util.Logger;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

/* loaded from: classes.dex */
public class DragAdapterx extends BaseAdapter {
    private static final String TAG = "DragAdapter";
    public List<AppInfo> channelList;
    private Context context;
    private int delete_position;
    private DragGridx grid;
    private int holdPosition;
    private boolean isItemShow = false;
    public String file_name = "app_list";
    private boolean isChanged = false;
    boolean isVisible = true;
    private boolean isDelete = false;
    private boolean xiezai = false;
    public int remove_position = -1;
    private Handler mHandler = new Handler();
    private int hideposition = -1;
    int len = 0;

    @Override // android.widget.Adapter
    public long getItemId(int i) {
        return i;
    }

    /* loaded from: classes.dex */
    class HolderView {
        private FrameLayout app_root;
        private TextView item_text;
        private ImageView iv_delete;
        private ImageView iv_icon;

        HolderView() {
        }
    }

    public boolean isDelete() {
        return this.isDelete;
    }

    public void setToDelete(boolean z) {
        this.xiezai = z;
    }

    public DragAdapterx(Context context, List<AppInfo> list, DragGridx dragGridx) {
        this.context = context;
        this.channelList = list;
        this.grid = dragGridx;
    }

    public DragAdapterx() {
    }

    public void setData(List<AppInfo> list) {
        this.channelList = list;
    }

    @Override // android.widget.Adapter
    public int getCount() {
        List<AppInfo> list = this.channelList;
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    @Override // android.widget.Adapter
    public AppInfo getItem(int i) {
        List<AppInfo> list = this.channelList;
        if (list == null || list.size() == 0) {
            return null;
        }
        return this.channelList.get(i);
    }

    public void showDeleteDialog(AppInfo appInfo, int i) {
        final Dialog dialog = new Dialog(this.context, R.style.ActionSheet);
        Window window = dialog.getWindow();
        View inflate = ((LayoutInflater) this.context.getSystemService("layout_inflater")).inflate(R.layout.delete_dialog, (ViewGroup) null);
        ((TextView) inflate.findViewById(R.id.delete_cancel)).setOnClickListener(new View.OnClickListener() { // from class: com.jxw.launcher.DragAdapterx.1
            @Override // android.view.View.OnClickListener
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        ((TextView) inflate.findViewById(R.id.delete_ok)).setOnClickListener(new AnonymousClass2(i, appInfo, dialog));
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.x = 0;
        attributes.y = 0;
        attributes.gravity = 17;
        dialog.onWindowAttributesChanged(attributes);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setContentView(inflate);
        dialog.show();
        window.setAttributes(attributes);
    }

    /* renamed from: com.jxw.launcher.DragAdapterx$2  reason: invalid class name */
    /* loaded from: classes.dex */
    class AnonymousClass2 implements View.OnClickListener {
        final /* synthetic */ Dialog val$dlg;
        final /* synthetic */ AppInfo val$iconInfo;
        final /* synthetic */ int val$position;

        AnonymousClass2(int i, AppInfo appInfo, Dialog dialog) {
            this.val$position = i;
            this.val$iconInfo = appInfo;
            this.val$dlg = dialog;
        }

        @Override // android.view.View.OnClickListener
        public void onClick(View view) {
            DragAdapterx.this.mHandler.post(new Runnable() { // from class: com.jxw.launcher.DragAdapterx.2.1
                @Override // java.lang.Runnable
                public void run() {
                    DragAdapterx.this.delete_position = AnonymousClass2.this.val$position;
                    Logger logger = Logger.getLogger();
                    logger.e("delete " + AnonymousClass2.this.val$position);
                    if (Common.isAnimaEnd) {
                        DragAdapterx.this.grid.deleteInfo(AnonymousClass2.this.val$position);
                        DragAdapterx.this.notifyDataSetChanged();
                        new Thread(new Runnable() { // from class: com.jxw.launcher.DragAdapterx.2.1.1
                            @Override // java.lang.Runnable
                            public void run() {
                                DragAdapterx.this.readContent(AnonymousClass2.this.val$iconInfo.packageName, AnonymousClass2.this.val$position);
                            }
                        }).start();
                    }
                }
            });
            this.val$dlg.dismiss();
        }
    }

    @Override // android.widget.Adapter
    public View getView(int i, View view, ViewGroup viewGroup) {
        HolderView holderView = new HolderView();
        View inflate = LayoutInflater.from(this.context).inflate(R.layout.item_appx, viewGroup, false);
        holderView.iv_icon = (ImageView) inflate.findViewById(R.id.app_icon);
        holderView.item_text = (TextView) inflate.findViewById(R.id.app_name);
        holderView.iv_delete = (ImageView) inflate.findViewById(R.id.delet_iv);
        holderView.app_root = (FrameLayout) inflate.findViewById(R.id.app_root);
        inflate.setTag(holderView);
        HolderView holderView2 = (HolderView) inflate.getTag();
        AppInfo item = getItem(i);
        holderView2.iv_icon.setBackground(item.getAppIcon());
        holderView2.item_text.setText(item.getAppLabel());
        holderView2.iv_delete.setOnClickListener(new AnonymousClass3(i, item));
        if (i == getCount() - 1) {
            if (view == null) {
                view = inflate;
            }
            view.setEnabled(false);
            view.setFocusable(false);
            view.setClickable(false);
        }
        if (this.isChanged && i == this.holdPosition && !this.isItemShow) {
            holderView2.item_text.setText("");
            holderView2.item_text.setSelected(true);
            holderView2.item_text.setEnabled(true);
            this.isChanged = false;
        }
        if (!this.isVisible && i == this.channelList.size() - 1) {
            holderView2.item_text.setText("");
            holderView2.item_text.setSelected(true);
            holderView2.item_text.setEnabled(true);
        }
        if (this.remove_position == i) {
            deletInfo(i);
        }
        if (!this.isDelete) {
            holderView2.iv_delete.setVisibility(8);
        } else if (TextUtils.isEmpty(item.packageName) || item.getIntent() != null) {
            holderView2.iv_delete.setVisibility(8);
        } else {
            holderView2.iv_delete.setVisibility(0);
        }
        if (this.hideposition == i) {
            inflate.setVisibility(4);
        } else {
            inflate.setVisibility(0);
        }
        return inflate;
    }

    /* renamed from: com.jxw.launcher.DragAdapterx$3  reason: invalid class name */
    /* loaded from: classes.dex */
    class AnonymousClass3 implements View.OnClickListener {
        final /* synthetic */ AppInfo val$iconInfo;
        final /* synthetic */ int val$position;

        AnonymousClass3(int i, AppInfo appInfo) {
            this.val$position = i;
            this.val$iconInfo = appInfo;
        }

        @Override // android.view.View.OnClickListener
        public void onClick(View view) {
            DragAdapterx.this.mHandler.post(new Runnable() { // from class: com.jxw.launcher.DragAdapterx.3.1
                @Override // java.lang.Runnable
                public void run() {
                    DragAdapterx.this.delete_position = AnonymousClass3.this.val$position;
                    Logger logger = Logger.getLogger();
                    logger.e("delsete " + AnonymousClass3.this.val$position + "__" + DragAdapterx.this.xiezai);
                    if (DragAdapterx.this.xiezai) {
                        new Thread(new Runnable() { // from class: com.jxw.launcher.DragAdapterx.3.1.1
                            @Override // java.lang.Runnable
                            public void run() {
                                Uri parse = Uri.parse("package:" + AnonymousClass3.this.val$iconInfo.packageName);
                                Intent intent = new Intent("android.intent.action.DELETE");
                                intent.setData(parse);
                                intent.addFlags(C.ENCODING_PCM_MU_LAW);
                                Log.e("11111", "222222");
                                DragAdapterx.this.context.startActivity(intent);
                                Log.e("position11", AnonymousClass3.this.val$position + "");
                            }
                        }).start();
                        return;
                    }
                    Log.e("deletehfq", AnonymousClass3.this.val$position + "已删除");
                    DragAdapterx.this.notifyDataSetChanged();
                    DragAdapterx.this.grid.deleteInfo(AnonymousClass3.this.val$position);
                    new Thread(new Runnable() { // from class: com.jxw.launcher.DragAdapterx.3.1.2
                        @Override // java.lang.Runnable
                        public void run() {
                            DragAdapterx.this.readContent(AnonymousClass3.this.val$iconInfo.packageName, AnonymousClass3.this.val$position);
                        }
                    }).start();
                }
            });
        }
    }

    private boolean isSystemApp(String str) {
        PackageInfo packageInfo;
        try {
            packageInfo = this.context.getApplicationContext().getPackageManager().getPackageInfo(str, 0);
        } catch (Throwable th) {
            Log.w(TAG, th.getMessage(), th);
            packageInfo = null;
        }
        if (packageInfo != null) {
            return ((packageInfo.applicationInfo.flags & 1) == 1) || ((packageInfo.applicationInfo.flags & 128) == 1);
        }
        return false;
    }

    public void setisDelete(boolean z) {
        this.isDelete = z;
    }

    public void deletInfo(int i) {
        this.channelList.remove(i);
        this.hideposition = -1;
        notifyDataSetChanged();
    }

    public void addItem(AppInfo appInfo) {
        this.channelList.add(appInfo);
        notifyDataSetChanged();
    }

    public void exchange(int i, int i2) {
        this.holdPosition = i2;
        AppInfo item = getItem(i);
        Log.d(TAG, "startPostion=" + i + ";endPosition=" + i2);
        if (i < i2) {
            this.channelList.add(i2 + 1, item);
            this.channelList.remove(i);
        } else {
            this.channelList.add(i2, item);
            this.channelList.remove(i + 1);
        }
        this.isChanged = true;
        notifyDataSetChanged();
    }

    public void setRemove(int i) {
        this.remove_position = i;
        notifyDataSetChanged();
    }

    public boolean isVisible() {
        return this.isVisible;
    }

    public void setVisible(boolean z) {
        this.isVisible = z;
    }

    public void setShowDropItem(boolean z) {
        this.isItemShow = z;
    }

    public void setHidePosition(int i) {
        this.hideposition = i;
        notifyDataSetChanged();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public String readContent(String str, int i) {
        File fileStreamPath = this.context.getFileStreamPath(this.file_name);
        if (!fileStreamPath.exists()) {
            return null;
        }
        try {
            BufferedReader bufferedReader = new BufferedReader(new FileReader(fileStreamPath));
            int i2 = this.len;
            String str2 = "";
            Logger.getLogger().e("pkg = " + str + "poi=" + i);
            while (true) {
                String readLine = bufferedReader.readLine();
                if (readLine != null) {
                    Logger.getLogger().e("line " + readLine);
                    if (!readLine.equals(str)) {
                        str2 = str2 + readLine + "\n";
                    }
                } else {
                    bufferedReader.close();
                    FileOutputStream fileOutputStream = new FileOutputStream(fileStreamPath, false);
                    Logger.getLogger().e("sb = " + str2);
                    fileOutputStream.write(str2.getBytes());
                    fileOutputStream.flush();
                    fileOutputStream.close();
                    return null;
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e2) {
            e2.printStackTrace();
            return null;
        }
    }

    public void delete_position() {
        this.grid.deleteInfo(this.delete_position);
        notifyDataSetChanged();
    }
}
